//
//  KLSlidingPicker.m
//  KeyLime
//
//  Created by Jesse Curry on 10/27/09.
//  Copyright 2009 Circonda, Inc. All rights reserved.
//

#import "KLSlidingPicker.h"

@implementation KLSlidingPickerRowSelection
@synthesize	row;
@synthesize component;
+ (KLSlidingPickerRowSelection*)rowSelectionWithRow: (NSInteger)row 
										  component: (NSInteger)component
{
	KLSlidingPickerRowSelection* rs = [[KLSlidingPickerRowSelection alloc] init];
	if ( rs )
	{
		rs.row = row;
		rs.component = component;
	}
	return [rs autorelease];
}
@end

////////////////////////////////////////////////////////////////////////////////////////////////////
@implementation KLSlidingPicker
@dynamic pickerDataSource;
@dynamic pickerDelegate;
@dynamic pickerShowsSelectionIndicator;
@dynamic text;

 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName: (NSString*)nibNameOrNil bundle: (NSBundle*)nibBundleOrNil 
{
    if ( self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil] ) 
	{
        rowSelections = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)dealloc 
{
	[rowSelections release];
	
    [super dealloc];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad 
{
    [super viewDidLoad];
	NSLog( @"KLSlidingPicker - viewDidLoad" );
	
	if ( pickerDataSource )
		picker.dataSource = pickerDataSource;
	else
		pickerDataSource = picker.dataSource;
	
	if ( pickerDelegate )
		picker.delegate = pickerDelegate;
	else
		pickerDelegate = picker.delegate;
	
	[picker reloadAllComponents];
	
	// If there were any selections made before this view loaded
	// apply them now.
	for ( KLSlidingPickerRowSelection* r in rowSelections )
	{
		[self selectRow: r.row 
			inComponent: r.component 
			   animated: NO];
	}
	[rowSelections removeAllObjects];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

#pragma mark -
#pragma mark KLSlidingViewController Overrides
//- (void)slideOn
//{
//	[picker reloadAllComponents];
//	[super slideOn];
//}

#pragma mark -
#pragma mark Dynamic Properties
- (id<UIPickerViewDataSource>)pickerDataSource
{
	return pickerDataSource;
}

- (void)setPickerDataSource: (id<UIPickerViewDataSource>)newPickerDataSource
{
	pickerDataSource = newPickerDataSource;
	[picker setDataSource: newPickerDataSource];
}

- (id<UIPickerViewDelegate>)pickerDelegate
{
	return pickerDelegate;
}

- (void)setPickerDelegate: (id<UIPickerViewDelegate>)newPickerDelegate
{
	pickerDelegate = newPickerDelegate;
	[picker setDelegate: newPickerDelegate];
	[picker reloadAllComponents];
}

- (BOOL)pickerShowsSelectionIndicator
{
	return [picker showsSelectionIndicator];
}

- (void)setPickerShowsSelectionIndicator: (BOOL)pickerShouldShowSelectionIndicator
{
	[picker setShowsSelectionIndicator: pickerShouldShowSelectionIndicator];
}

- (NSInteger)selectedRowInComponent: (NSInteger)theComponent
{
	return [picker selectedRowInComponent: theComponent];
}

- (void)selectRow: (NSInteger)row 
	  inComponent: (NSInteger)component 
		 animated: (BOOL)animated
{
	if ( picker )
	{
		[picker selectRow: row 
			  inComponent: component 
				 animated: animated];
	}
	else
	{
		[rowSelections addObject: [KLSlidingPickerRowSelection rowSelectionWithRow: row 
																		 component: component]];
	}
}

#pragma mark -
#pragma mark UIPickerViewDataSource Methods
- (NSInteger)numberOfComponentsInPickerView: (UIPickerView*)pickerView
{
	return 1;
}

- (NSInteger)pickerView: (UIPickerView*)pickerView 
numberOfRowsInComponent: (NSInteger)component
{
	return 1;
}

#pragma mark -
#pragma mark UIPickerViewDelegate Methods
- (NSString*)pickerView: (UIPickerView*)pickerView 
			titleForRow: (NSInteger)row 
		   forComponent: (NSInteger)component
{
	return @"Configure Me";
}

#pragma mark -
#pragma mark Properties
- (NSString*)text
{
	NSInteger componentToReturn = 0;
	
	if ( [picker.dataSource numberOfComponentsInPickerView: picker] == 1 )
		return [picker.delegate pickerView: picker 
							   titleForRow: [picker selectedRowInComponent: componentToReturn] 
							  forComponent: componentToReturn];
	else
		return @"";
}

@end
